package com.shanzhu.book.web;

import com.shanzhu.book.model.Notice;
import com.shanzhu.book.model.SeatReservation;
import com.shanzhu.book.service.SeatReservationService;
import com.shanzhu.book.utils.PageUtils;
import com.shanzhu.book.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 座位预约 控制层
 *
 * @date: 2025-07-10
 */
@RestController
@RequestMapping("/seat")
public class SeatReservationController {

    @Resource
    private SeatReservationService seatReservationService;

    /**
     * 预约座位
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @PostMapping("/reserve")
    public Map<String, Object> reserveSeat(@RequestBody SeatReservation reservation) {
        try {
            boolean success = seatReservationService.reserveSeat(reservation);
            return success ? R.getResultMap(200, "预约请求已提交") :
                    R.getResultMap(420, "座位不可用");
        } catch (Exception e) {
            return R.getResultMap(500, "预约失败: " + e.getMessage());
        }
    }

    /**
     * 取消预约
     *
     * @param id     预约ID
     * @param userId 用户ID
     * @return 结果
     */
    @PostMapping("/cancel/{id}")
    public Map<String, Object> cancelReservation(@PathVariable Long id,
                                                 @RequestParam Long userId) {
        boolean success = seatReservationService.cancelReservation(id, userId);
        return success ? R.getResultMap(200, "取消预约成功") :
                R.getResultMap(420, "无法取消预约");
    }

    /**
     * 分页查询用户
     *
     * @param params {page, limit, noticename}
     * @return 用户信息
     */
    @GetMapping(value = "/queryNoticesByPage")
    public Map<String, Object> queryNoticesByPage(@RequestParam Map<String, Object> params) {
        PageUtils.parsePageParams(params);
        int count = seatReservationService.getSearchCount(params);
        List<SeatReservation> notices = seatReservationService.searchReservations2(params);
        return R.getListResultMap(0, "success", count, notices);
    }


    /**
     * 获取用户所有预约记录
     *
     * @param userId 用户ID
     * @return 预约列表
     */
    @GetMapping("/user/{userId}")
    public Map<String, Object> getUserReservations(@PathVariable Long userId) {
        List<SeatReservation> reservations = seatReservationService.getUserReservations(userId);
        return R.getResultMap(200, "获取成功", reservations);
    }

    /**
     * 获取用户当前预约
     *
     * @param userId 用户ID
     * @return 当前预约
     */
    @GetMapping("/current/{userId}")
    public Map<String, Object> getCurrentReservation(@PathVariable Long userId) {
        SeatReservation reservation = seatReservationService.getCurrentReservation(userId);
        return reservation != null ? R.getResultMap(200, "获取成功", reservation) :
                R.getResultMap(404, "无当前预约");
    }


    @DeleteMapping(value = "/deleteSeat")
    public Integer deleteSeat(@RequestBody SeatReservation seat, HttpServletRequest request) {
        System.out.println("request = " + request);
        return seatReservationService.deleteSeatReservation(seat);
    }

    @PostMapping(value = "/addSeat")
    public Integer addSeat(@RequestBody SeatReservation notice, HttpServletRequest request) {
        System.out.println("request = " + request);
        return seatReservationService.addSeat(notice);
    }



    /**
     * 分页搜索预约记录
     *
     * @param params 查询参数 {page, limit, seatNumber, location, status}
     * @return 分页结果
     */
    @GetMapping("/search")
    public Map<String, Object> searchReservations(@RequestParam Map<String, Object> params) {
        PageUtils.parsePageParams(params);
        List<SeatReservation> list = seatReservationService.searchReservations2(params);

        int count = seatReservationService.getSearchCount(params);

        return R.getListResultMap(0, "success", count, list);
    }

    /**
     * 签到
     *
     * @param id     预约ID
     * @param userId 用户ID
     * @return 结果
     */
    @PostMapping("/checkin/{id}")
    public Map<String, Object> checkIn(@PathVariable Long id,
                                       @RequestParam Long userId) {
        boolean success = seatReservationService.checkIn(id, userId);
        return success ? R.getResultMap(200, "签到成功") :
                R.getResultMap(420, "签到失败");
    }

    /**
     * 签退
     *
     * @param id     预约ID
     * @param userId 用户ID
     * @return 结果
     */
    @PostMapping("/checkout/{id}")
    public Map<String, Object> checkOut(@PathVariable Long id,
                                        @RequestParam Long userId) {
        boolean success = seatReservationService.checkOut(id, userId);
        return success ? R.getResultMap(200, "签退成功") :
                R.getResultMap(420, "签退失败");
    }

    /**
     * 获取座位预约数量
     *
     * @return 数量
     */
    @GetMapping("/count")
    public Map<String, Object> getReservationCount() {
        int count = seatReservationService.selectCount();
        return R.getResultMap(200, "获取成功", count);
    }

    @RequestMapping(value = "/handleStatus")
    public Integer updateSeatStatus(@RequestBody SeatReservation seat) {
        return seatReservationService.updateSeatStatus(seat);
    }

}